var $editDialog;
require(['grid','topBar', 'date','layout','select2'], function () {
    var $grid=$("#grid"),$top = $('#top'),reloadGridData,lineIds, carIds, drvIds,equipOnlineDialog;

    //顶部栏初始化
    function topBarInit() {
        $('#top').topBar({
            left: [
                {
                    type: 'filter-more',
                    dateType: 'custom',
                    search: function () {
                        reloadGridData();
                    },
                    item: [
                        {
                            text: '时间快捷',
                            name1: 'beginTime',
                            name2: 'endTime',
                            type: 'dateLine'
                        }, {
                            text: '下线次数>=',
                            name: 'offlineCount',
                            width: 100,
                            type: 'select',
                            data:[
                                {text: '全部', value: ''},
                                {text: '10次', value: '10'},
                                {text: '20次', value: '20'},
                                {text: '30次', value: '30'},
                                {text: '40次', value: '40'},
                                {text: '50次', value: '50'},
                                {text: '60次', value: '60'},
                                {text: '70次', value: '70'},
                                {text: '80次', value: '80'},
                                {text: '90次', value: '90'},
                                {text: '100次', value: '100'}
                            ]
                        },{
                            text: '下线时长>=',
                            name: 'offlineTime',
                            width: 100,
                            type: 'select',
                            data:[
                                {text: '全部', value: ''},
                                {text: '1小时', value: '1'},
                                {text: '2小时', value: '2'},
                                {text: '3小时', value: '3'},
                                {text: '4小时', value: '4'},
                                {text: '5小时', value: '5'},
                                {text: '6小时', value: '6'},
                                {text: '7小时', value: '7'},
                                {text: '8小时', value: '8'}
                            ]
                        },{
                            text: '离线率>=',
                            name: 'offlineRate',
                            width: 100,
                            type: 'select',
                            data:[
                                {text: '全部', value: ''},
                                {text: '10%', value: '10'},
                                {text: '20%', value: '20'},
                                {text: '30%', value: '30'},
                                {text: '40%', value: '40'},
                                {text: '50%', value: '50'},
                                {text: '60%', value: '60'},
                                {text: '70%', value: '70'},
                                {text: '80%', value: '80'}
                            ]
                        }, {
                            text: '设备编号',
                            name: 'seldevId',
                            width: 80,
                            type: 'text'
                        },
                    ]
                }
            ],
            right: [
                {
                    text: '导出',
                    type: 'export-btn',
                    authority: '',
                    item: [
                        {
                            text: 'Excel导出',
                            click: function () {
                                exportFile('Excel');
                            }
                        },
                        {
                            text: 'PDF导出',
                            click: function () {
                                exportFile('Pdf');
                            }
                        }
                    ]
                }
            ]
        });
    }

    //grid初始化
    function gridInit() {
	    $grid.grid({
			url: "/deviceOnline/getDeviceOnlineStatus.htm",
			colModel:[
				{name: "carId", label: "carId", hidden: true},
				{name: "rcrdDate", label: "数据日期" },
                {name: "licPltNo", label: "车牌号"},
				{name: "lineName", label: "运营线路"},
				{name: "eqmtCd", label: "设备ID"},
				{name: "simCardno", label: "SIM卡号"},
				{name: "minRcrdTime", label: "最早上线时间"},
                {name: "maxRcrdTime", label: "最后下线时间"},
                {name: "totalTime", label: "总时长"},
                {name: "offlineCount", label: "下线次数"},
                {name: "offlineTime1", label: "下线时长hidden", hidden: true},
                {name: "offlineTime", label: "下线时长",sorttype : "text",formatter:function (cellvalue, options, rowObject){
                    if(parseInt(rowObject.offlineTime1) >= 7200){
                        return" <em style='color: red'>"+rowObject.offlineTime+"</em>"
                    }else{
                        return" <em>"+(rowObject.offlineTime||'')+"</em>"
                    }
                }},
                {name: "offlineRate1", label: "离线率hidden", hidden: true},

                {name: "offlineRate", label: "离线率",sorttype : "text",formatter:function (cellvalue, options, rowObject) {
                	if(parseInt(rowObject.offlineRate1) >= 20){
                        return" <a style='color:red' href='javascript:void(0)' class='text-link' onclick=offLineRateDetailed(\""
                        +rowObject.carId+"\",\""+rowObject.rcrdDate+"\");>"+rowObject.offlineRate+"</a>"
                    }else{
                    	if (rowObject.offlineRate=='%'){
                    		rowObject.offlineRate=''
                    	}
                    	return" <a  href='javascript:void(0)' class='text-link' onclick=offLineRateDetailed(\""
                        +rowObject.carId+"\",\""+rowObject.rcrdDate+"\");>"+(rowObject.offlineRate||'')+"</a>"
                   }
				}
                },
                {name: "carOperate", label: "图表分析", sortable:false,align: "center",formatter:function(cellvalue, options, rowObject){
                    return "<a href='javascript:void(0)' class='operate operate-qushi' onclick=carOnlineAnalysis(\""
                        +rowObject.carId+"\",\""+rowObject.licPltNo+"\",\""+rowObject.rcrdDate+"\");>设备在线分析</a><div class='bor'></div>";
                },cellattr: function(rowId, tv, rawObject, cm, rdata) {
                    return 'id=\'carOperate' + rowId + "\'";
                }
                },
                {name: "exportOperate", label: "导出明细", sortable:false,align: "center",formatter:function(cellvalue, options, rowObject){
                    return "<a href='javascript:void(0)' class='operate operate-daochu' onclick=exportOnlineAnalysis(\""
                        +rowObject.carId+"\",\""+rowObject.rcrdDate+"\");>导出明细</a><div class='bor'></div>";
                }
                }
            ],
            multiselect: false
		});
    }

    carOnlineAnalysis = function (carId,licPltNo,rcrdDate) {
        var data = new Array();
        var obj = new Object();
        obj.carId = carId;
        obj.licPltNo = licPltNo;
        obj.rcrdDate = rcrdDate;

        data.push(obj);
        openEquipOnlineDialog("设备在线情况", data); //弹出的标题和数据
    };
    function openEquipOnlineDialog(title, data) {
        if (!equipOnlineDialog) {
            equipOnlineDialog = $('<div>').dialog({
                title: title,
                url: './service/report/equipOnlineAnalysis.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.9
            });
        }
        equipOnlineDialog.dialog('open');
        equipOnlineDialog.dialog('exec', 'loadData', data);
    }
    
    offLineRateDetailed = function(carId,rcrdDate){
    	getEditDialog().dialog('exec', 'accept',carId,rcrdDate);;
    }

    function exportFile(suffix) {
        if ($grid.getGridParam("reccount") > 0) {
            var url = "/deviceOnline/exportDeviceOLStatus" + suffix + ".htm";
            var type = commonFun.getLeftTreeCurrentTreeType();
            var orgIds = commonFun.getLeftTreeCheckedNodes(type);
            checkIds();
            if (!orgIds) {
                $.messager.alert("提示", "请选择查询车辆/机构/驾驶员。", "info");
            } else {
                var $postData = $("#top").parent().getValues();
                $postData.type = type;
                $postData.orgIds = orgIds.toString();
                $postData.carIds = carIds;
                $postData.lineIds = lineIds;
                $.exportFile(url, $postData);
            };

        }else {
            $.messager.alert("提示", "请先查询数据。", "info");
        };
    }
    exportOnlineAnalysis = function (carId,rcrdDate) {
    //function exportOnlineAnalysis(carId,rcrdDate) {
        var url = "/deviceOnline/exportOnlineAnalysis.htm";
        var $postData = {};
        $postData.carId = carId;
        $postData.rcrdDate = rcrdDate;
        $.exportFile(url, $postData);
    }


    //查询grid
    reloadGridData = function(){
        var $postData = $("#top").parent().getValues();
        // 左侧树tab选中的类型
        var type = commonFun.getLeftTreeCurrentTreeType();
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        checkIds();
        if (!orgIds) {
            $.messager.alert("提示", "请选择查询车辆/机构/驾驶员！", "info");
        } else {
            $postData.type = type;
            $postData.orgIds = orgIds.toString();
            $postData.carIds = carIds;
            $postData.lineIds = lineIds;
            $('#grid').grid('setGridParam', {
                datatype: "json",
                postData: $postData
            }).trigger("reloadGrid");
        };
    };

    
    //
    function getEditDialog(fn){
			 if(!$editDialog){
		        $editDialog = $('<div>').dialog({
		        	title: '导入电子更纸',
		            url: './service/setting/funOffLineRateSet.htm',
		            height: 450,
		            width: 1000,
		            autoOpen: true,
		            buttons: [
	                {
	                    text: '关闭',
	                    css: 'btn-cancel',
	                    click: function () {
	                        $(this).dialog('close');
	                    }
	                }
	            ]
		    });	
		}
			 $editDialog.dialog('open');
				return $editDialog;
	};
    //获取左侧选择的节点信息
    function checkIds(){
        ///清空节点参数
        lineIds = "";
        carIds = "";
        drvIds = "";
        var includeIds = false;//是否选择了节点
        var nodes = commonFun.getLeftTreeCheckedNodes();
        if(nodes.CAR){//是否包含carIds
            carIds = nodes.CAR.toString();
            includeIds = true;
        }
        if(nodes.LINE){//是否包含lineIds
            lineIds = nodes.LINE.toString();
            includeIds = true;
        }
        return includeIds;
    }


    //layout 布局
    $('body').layout();

    topBarInit();
    gridInit();
});